//
//  ViewController.m
//  通讯录
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "AddressBookCell.h"
#import "AddressBookModel.h"
#import "InfoController.h"

#define kCell_AddressBook_ReuseIdentifier @"AddressBookCell"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createTableView];
}

#pragma mark - get data
- (void)handleData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    NSArray *result = [NSArray arrayWithContentsOfFile:path];
    self.dataArr = [NSMutableArray array];
    for (NSDictionary *dic in result) {
        AddressBookModel *model = [[AddressBookModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataArr addObject:model];
    }
    
    
}
#pragma mark - create table view
- (void)createTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    // register cell
    [_tableView registerClass:[AddressBookCell class] forCellReuseIdentifier:kCell_AddressBook_ReuseIdentifier];
    
}

#pragma mark - table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell_AddressBook_ReuseIdentifier];
    
    cell.textLabel.text = [_dataArr[indexPath.row] name];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddressBookModel *model = _dataArr[indexPath.row];
    InfoController *infoVC = [[InfoController alloc]init];
    infoVC.name = model.name;
    infoVC.age = model.age;
    infoVC.sex = model.gender;
    infoVC.phoneNumber = model.phoneNumber;
    [self presentViewController:infoVC animated:YES completion:nil];
}


#pragma mark - table view delegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc]init];
    label.text = @"通讯录";
    label.font = [UIFont boldSystemFontOfSize:20];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}


#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
